/**
* 系统管理 角色管理
*/
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/index/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>角色管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="role-search">
      <el-form-item label="">
        <el-input size="small" v-model="formInline.roleName" placeholder="输入角色名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" @selection-change="selectChange" :data="roleData" highlight-current-row v-loading="loading"
      element-loading-text="拼命加载中" style="width: 100%">
      <el-table-column type="selection" width="50"> </el-table-column>
      <el-table-column sortable prop="roleId" label="角色ID" width="300">
      </el-table-column>
      <el-table-column sortable prop="roleName" label="角色名称" width="300">
      </el-table-column>
      <el-table-column sortable prop="remark" label="角色描述" width="300">
      </el-table-column>
      
      <el-table-column label="操作" min-width="100">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteRole(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click='closeDialog("edit")'>
      <el-form label-width="120px" :model="editForm" ref="editForm" :rules="rules">
        
        <el-form-item label="角色名称" prop="roleName">
          <el-input size="small" v-model="editForm.role.roleName" auto-complete="off" placeholder="请输入角色名称"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="remark">
          <el-input size="small" v-model="editForm.role.remark" auto-complete="off" placeholder="请输入角色描述"></el-input>
        </el-form-item>
      </el-form>
      <el-tree ref="tree" default-expand-all="" :data="menuList" :props="menuProps"
         node-key="menuId" show-checkbox>
      </el-tree>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog('edit')">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  // 导入请求方法
  import {
    roleList,
    roleInsert,
    roleUpdate,
    roleDelete,
    menuTree
  } from "@/api/role";
  import Pagination from "@/components/Pagination";
  export default {
    data() {
      return {
        loading: false, //是否显示加载
        title: "添加",
        editFormVisible: false, //控制编辑页面显示与隐藏
        // 编辑与添加form表单
        editForm: {
          role: {
            roleId: '',
            remark: '',
            roleName: ''
          }
        },
        editForm_1: {
          role: {
            roleId: 0,
            remark: null,
            roleName: null
          }
        },
        
        // 角色对象
        role: {
          roleId: "",
          roleName: "",
        },
        checkrole: [],
        listData: [], //用户数据
        menuList: [], //菜单数据
        checkMenu: [], //选中的菜单
        menuProps:{
          label: 'menuName',
          children: 'menus'
        },
        // rules表单验证
        rules: {
          roleName: [{
            required: true,
            message: "请输入角色名",
            trigger: "blur",
          }, ],
          roleId: [{
            required: true,
            message: "请选择角色",
            trigger: "blur",
          }, ],
          
        },
        // 搜索用参数
        formInline: {
          pageNum: 1,
          pageSize: 10,
          roleName: "",
        },
        //用户列表数据
        roleData: [],
        defaultProps: {
          children: "children",
          label: "name",
        },
        // 选中
        checkrole: [],
        // 分页参数
        pageparm: {
          currentPage: 1,
          pageSize: 5,
          total: 3,
        },
      };
    },
    // 注册组件
    components: {
      Pagination,
    },
    /**
     * 数据发生改变
     */
    watch: {},

    created() {
      this.getdata(this.formInline) //获得角色列表
      this.getMenuList() //获得菜单列表
    },
    /**
     * 使用api获得roleList
     */
    mounted() {
      this.getdata(this.formInline);
    },

    /**
     * 里面的方法只有被调用才会执行
     */
    methods: {
      // 获取数据方法
      getdata(parameter) {
        this.loading = true;
        // 获取菜单列表
        roleList(parameter).then((res) => {
          this.loading = false;
          if (res.code != 200) {
            this.$message({
              type: "error",
              message: res.message,
            });
          } else {
            this.roleData = res.data.list;
            // 分页赋值
            this.pageparm.currentPage = this.formInline.pageNum;
            this.pageparm.pageSize = this.formInline.pageSize;
            this.pageparm.total = res.data.total;
          }
        });
      },
      getMenuList() {
        menuTree()
          .then(res => {
            if (res.code == 200) {
              console.log(res)
              this.menuList = res.data
            } else {
              this.$message.info(res.message)
            }
          })
          .catch(err => {
            this.$message.error(res.message)
          })
      },
      // 分页插件事件
      callFather(parm) {
        this.formInline.page = parm.currentPage;
        this.formInline.limit = parm.pageSize;
        this.getdata(this.formInline);
      },
      //搜索事件->[暂时不做]
      search() {
        this.getdata(this.formInline);
        
      },
      // 选择复选框事件
      selectChange(val) {
        this.selectdata = val;
      },
      //显示编辑界面
      handleEdit: function (index, row) {
        //Ajax获得该角色的菜单信息
        //选中该菜单
        this.editFormVisible = true
        if (row != undefined && row != 'undefined') {
          this.title = '修改'
          this.editForm.role.roleId = row.roleId
          this.editForm.role.remark = row.remark
          this.editForm.role.roleName = row.roleName
        } else {
          this.title = '添加'
          this.editForm.role.remark = ''
          this.editForm.role.roleName = ''
        }
      },
      // 编辑、添加提交方法
     submitForm(editData) {
        this.$refs[editData].validate(valid => {
          if (valid) {
            roleInsert(this.editForm)
              .then(res => {
                this.editFormVisible = false
                this.loading = false
                if (res.success) {
                  this.getdata(this.formInline)
                  this.$message({
                    type: 'success',
                    message: '角色保存成功！'
                  })
                } else {
                  this.$message({
                    type: 'info',
                    message: res.msg
                  })
                }
              })
              .catch(err => {
                this.editFormVisible = false
                this.loading = false
                this.$message.error('角色保存失败，请稍后再试！')
              })
          } else {
            return false
          }
        })
      },
      // 关闭编辑、增加弹出框
      closeDialog(dialog) {
        if (dialog == "edit") {
          this.editFormVisible = false;
        }
      },
      // 删除菜单
      deleteRole(index, row) {
        this.$confirm('确定要删除吗?', '信息', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          //调用roleDelete删除角色信息
          let roleId = row.roleId;
          roleDelete({
            roleId
          }).then(res => {
            if (res.code != 200) {
              this.$message({
                type: 'error',
                message: res.message
              })
            } else {
              this.$message({
                type: 'success',
                message: '角色删除成功！'
              })
              this.getdata(this.formInline) //重新获取列表
            }
          })
        })
      },
    }
  };
</script>

<style scoped>
  .role-search {
    margin-top: 20px;
  }

  .roleRole {
    width: 100%;
  }
</style>